"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameManager = void 0;
const RoomResultDao_1 = require("../../hall_server/dao/RoomResultDao");
const LogServerHelper_1 = require("../../log_server/LogServerHelper");
const SocketConnectionManager_1 = require("../engine/socketserver/socket/SocketConnectionManager");
const DateUtil_1 = require("../engine/utils/DateUtil");
const RandomUtil_1 = require("../engine/utils/RandomUtil");
const ResponseModel_1 = require("../model/ResponseModel");
const RoomManager_1 = require("./RoomManager");
class GameManager {
    create_game_result(room) {
        let gameResult = room.gameResult;
        gameResult.gameType = room.gameType;
        gameResult.room_id = room.room_id;
        gameResult.clubid = room.clubid;
        gameResult.gameConf = room.conf;
        gameResult.playerList = room.playerList;
        gameResult.time = DateUtil_1.default.now();
        let bigWinScore = 0;
        let bigWinList = [];
        for (let i = 0; i < gameResult.playerList.length; i++) {
            let player = gameResult.playerList[i];
            if (player.score > bigWinScore) {
                bigWinScore = player.score;
                bigWinList = [player.userid];
            }
            else if (player.score == bigWinScore) {
                bigWinList.push(player.userid);
            }
            if (i == 0) {
                gameResult.userid1 = player.userid;
            }
            else if (i == 1) {
                gameResult.userid2 = player.userid;
            }
            else if (i == 2) {
                gameResult.userid3 = player.userid;
            }
            else if (i == 3) {
                gameResult.userid4 = player.userid;
            }
            else if (i == 4) {
                gameResult.userid5 = player.userid;
            }
        }
        gameResult.bigWin = RandomUtil_1.default.randomItem(bigWinList);
        RoomManager_1.RoomManager.instance.decRoomDayingjiaCost(room, gameResult.bigWin);
        return gameResult;
    }
    add_game_result(room) {
        return __awaiter(this, void 0, void 0, function* () {
            let gameResult = this.create_game_result(room);
            if (gameResult.id > 0) {
                yield RoomResultDao_1.RoomResultDao.update_room_result(gameResult);
            }
            else {
                gameResult.id = yield RoomResultDao_1.RoomResultDao.insert_room_result(gameResult);
            }
        });
    }
    notify_game_result(room) {
        return __awaiter(this, void 0, void 0, function* () {
            let gameResult = this.create_game_result(room);
            let endModel = new ResponseModel_1.ResponseModel();
            let endObj = {
                "gameResult": gameResult,
            };
            endModel.data = endObj;
            for (let i = 0; i < gameResult.playerList.length; i++) {
                let player = room.playerList[i];
                if (player) {
                    let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                    if (conn) {
                        // conn.createMsgToClient(0, ProtoConfig.notify_game_result, endModel)
                    }
                    if (gameResult.clubid > 0) {
                        let wintimes = 0;
                        if (player.score > 0) {
                            wintimes++;
                        }
                        LogServerHelper_1.LogServerHelper.log_club_top(gameResult.clubid, player.userid, player.score, wintimes, 1);
                    }
                }
            }
            if (gameResult.id > 0) {
                yield RoomResultDao_1.RoomResultDao.update_room_result(gameResult);
            }
            else {
                gameResult.id = yield RoomResultDao_1.RoomResultDao.insert_room_result(gameResult);
            }
            return gameResult;
        });
    }
}
exports.GameManager = GameManager;
GameManager.instance = new GameManager();
